$(function () {
    //全局定义页数与总条数
    var markid = getUrlString('markid');
    var page = -1,totalSize = 0,key = '';

    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                window.location.reload();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                danjiList(page)
            }
        }
    });
    $('#goBack').click(function () {
        nativeCommand("dancool://activity=fin")
    });

    //搜索
    $('#searchBtn').click(function () {
        key = $('#searchVal').val();
        if(key === '' || key === undefined){
           laymsg('搜索内容不能为空');
            return
        }
        $('#newBestList').empty();//清空容器;
        totalSize = 0;
        page = 0;
        danjiList(page)
    });

    function danjiList(page) {
        if(key === '' || key === undefined){
            $('#mrefresh').hide();
            return
        }else {
            $('#mrefresh').show()
        }
        var data = {
            'markid':markid,
            'djtype':'0,1,4,7,6,8',
            'key':key,
            'page':page,
            'djsort':'time',
            'is_wx':1
        };
        getData('get', '/niu/apiv4/niu_getpoollist.php', data, function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                var append = '';
                $(data.data).each(function () {
                    if (this.is_parent === '1'){
                        append+='<li onclick="window.location.href = \'dancool://fullweb='+urlView+'danji/danji.html?danjiid='+this.id+'&is_parent=1\'">' ;
                    }else {
                        append+='<li onclick="window.location.href = \'dancool://fullweb='+urlView+'danji/danji.html?danjiid='+this.id+'&is_parent=3\'">' ;
                    }

                    append+='<input type="hidden" class="id" value="'+this.id+'">';
                    if (this.is_parent === '1' && this.CountSum === this.CountFinish){
                        append += '<div class="posTop">' +
                            '   <img src="../res/img/common/picbj.png" alt="">' +
                            '</div>'
                    } else {
                        if (this.leftcount === '0'){
                            append += '<div class="posTop">' +
                                '   <img src="../res/img/common/picbj.png" alt="">' +
                                '</div>'
                        }
                    }
                    append += '                   <img class="picBanner" src="'+this.picbanner+'" alt="">' +
                        '                    <div class="name">'+this.name+'</div>' +
                        '                    <div class="price">' +
                        '                        <span>'+this.cuxiaotext+'</span>' ;
                    if (this.is_parent === '1'){
                        append += '    <span class="more">多套</span>'
                    }
                    append +='                        <div class="money">' +
                        '                           <img src="../res/img/common/goldCoin.png" alt=""> ' +
                        '                           <span>'+this.money2+'</span>' +
                        '                        </div>' +
                        '                    </div>' +
                        '                </li>' ;
                });
                $('#newBestList').append(append);


                totalSize = totalSize + 10;
                if (totalSize > data.datacount)
                    mRefresh.endUpLoading(true);
                else{
                    mRefresh.endUpLoading(false);
                }

                // $('#newBestList li').on("click",function () {
                //     var id = $(this).find('.id').val();
                //     console.log(id);
                //     window.location.href = 'dancool://danji='+id;
                // });

            }else {
                laymsg(data.msg)
            }

        });
    }


});
